/***
 * * PROJECT
 * *    Logical Neighborhood Routing
 * * VERSION
 * *    $Revision: 1.9 $
 * * DATE
 * *    $Date: 2006/12/14 18:59:28 $
 * * LOG
 * *    $Log: SSD.java,v $
 * *    Revision 1.9  2006/12/14 18:59:28  lmottola
 * *    Added comments.
 * *
 * *    Revision 1.8  2006/11/03 23:09:35  lmottola
 * *    Changed nodeAttributes package.
 * *
 * *    Revision 1.7  2006/11/03 23:08:12  lmottola
 * *    Traffic application beta running.
 * *
 * *    Revision 1.6  2006/10/27 01:28:27  lmottola
 * *    Debugging support added.
 * *
 * *    Revision 1.5  2006/10/26 01:06:45  lmottola
 * *    Added random timeouts on retransmission.
 * *
 * *    Revision 1.4  2006/10/25 22:50:27  lmottola
 * *    SSD routing debugged,
 * *
 * *
 * *
 * *   Logical Neighborhoods Routing - Java Version
 * *                                
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.runtime.ln;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

import atag.runtime.NodeInfo;
import atag.runtime.ln.messages.ProfAdv;
import atag.runtime.ln.nodeAttributes.NodeAttribute;

/**
 * @author Luca Mottola <a
 *         href="mailto:mottola@elet.polimi.it">mottola@elet.polimi.it</a>
 * 
 */
public class SSD {

	private static long idEntryCounter = 0;

	private NeighborSet neighborSet;

	private NodeInfo nodeInfo;

	private LogicalNeighborhood ln;

	private Vector ssdData;

	public SSD(NodeInfo nodeInfo, NeighborSet neighborSet,
			LogicalNeighborhood ln) {

		this.nodeInfo = nodeInfo;
		this.ln = ln;
		this.ssdData = new Vector();
		this.neighborSet = neighborSet;
	}

	protected void parseProfAdv(ProfAdv adv, long ssdTime) {

		// TODO: Add links between entries to compress State Space
		boolean modified = false;
		adv.setCost(adv.getCost() + nodeInfo.getSendingCost());
		Iterator profAdvIt = new Vector(adv.getNodeAttributes()).iterator();
		while (profAdvIt.hasNext()) {
			NodeAttribute profAdvAttr = (NodeAttribute) profAdvIt.next();
			if (needsInsertion(profAdvAttr, adv.getCost())) {
				modified = true;
				ssdData.add(new SSDAttribute(idEntryCounter++, profAdvAttr, adv
						.getCost(), adv.getOriginatorId(), ssdTime));
			}
		}
		if (modified) {
			nodeInfo.debugPrint("Propagating node profile for "
					+ adv.getOriginatorId() + " received from "
					+ adv.getSenderId());
			ln.forwardMessage(adv);
		}
	}

	private void pruneExpiredEntries(long ssdTime) {
		HashSet toBeRemoved = new HashSet();
		Iterator it = ssdData.iterator();
		while (it.hasNext()) {
			SSDAttribute ssdAttr = (SSDAttribute) it.next();
			if (ssdAttr.expirationTime > ssdTime) {
				toBeRemoved.add(ssdAttr);
			}
		}
		ssdData.removeAll(toBeRemoved);
	}

	protected void disseminateProfAdv(long ssdTime) {
		ProfAdv profAdv = new ProfAdv(nodeInfo.getMyId(), ssdTime, new Vector(
				nodeInfo.getNodeAttributes()));
		nodeInfo.debugPrint("Disseminating node profile at time " + ssdTime);
		ln.sendMessage(profAdv);
	}

	private boolean needsInsertion(NodeAttribute attribute, int cost) {

		Iterator it = ssdData.iterator();
		while (it.hasNext()) {
			SSDAttribute ssdAttr = (SSDAttribute) it.next();
			if (ssdAttr.attribute.equals(attribute) && ssdAttr.cost < cost) {
				return false;
			}
		}
		return true;
	}

	class SSDAttribute {

		private long entryId;

		private NodeAttribute attribute;

		private int cost;

		private int decreasingPath;

		private Vector increasingPaths;

		private int originatorId;

		private long expirationTime;

		public SSDAttribute(long entryId, NodeAttribute attr, int cost,
				int originatorId, long ssdTime) {

			this.entryId = entryId;
			this.attribute = attr;
			this.cost = cost;
			this.originatorId = originatorId;
			this.expirationTime = ssdTime
					+ LogicalNeighborhood.EXPIRATION_TICKS;
		}

		protected void update(long ssdTime) {
			this.expirationTime = ssdTime
					+ LogicalNeighborhood.EXPIRATION_TICKS;
		}
	}
}
